package service;

import java.io.IOException;
import java.util.List;
import java.util.Vector;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;

import ejb.CandidateCommentEJBRemote;
import ejb.CandidateEJBRemote;
import entityJPA.Candidatecomment;
import entityJPA.Candidate;
import entityJPA.Position;
import entityJPA.User;

/**
 * Session Bean implementation class CandidateService
 */
@Stateless
@LocalBean
public class CandidateService {

	@EJB
	private CandidateEJBRemote candEJB;

	@EJB
	private CandidateCommentEJBRemote candComEJB;

	@EJB
	private PositionService posService;

	private static Logger logger = Logger
			.getLogger(UserService.class.getName());

	/**
	 * Default constructor.
	 */
	public CandidateService() {
		// TODO Auto-generated constructor stub

		FileHandler fh;
		try {
			fh = new FileHandler("C:/CandidateLoggerFile.log", true);
			SimpleFormatter formatter = new SimpleFormatter();
			fh.setFormatter(formatter);
			logger.addHandler(fh);
		} catch (SecurityException | IOException e) {
			e.printStackTrace();
		}
	}

	public void addCandidate(Candidate cand) throws Exception {
		try {
			candEJB.persistCandidate(cand);
			logger.info("inserted user:" + cand.toString());
		} catch (Exception e) {
			logger.log(Level.SEVERE, e.getMessage());
		}
	}

	public void editCandidate(Candidate cand) throws Exception {
		try {
			candEJB.mergeCandidate(cand);
			logger.info("inserted user:" + cand.toString());
		} catch (Exception e) {
			logger.log(Level.SEVERE, e.getMessage());
		}
	}

	public void removeCandidate(Candidate cand) throws Exception {
		try {
			candEJB.removeCandidate(cand);
			logger.info("inserted user:" + cand.toString());
		} catch (Exception e) {
			logger.log(Level.SEVERE, e.getMessage());
		}
	}

	public void removeCandidateComment(Candidatecomment candCom)
			throws Exception {
		try {
			candComEJB.removeCandidatecomment(candCom);
			logger.info("inserted user:" + candCom.toString());
		} catch (Exception e) {
			logger.log(Level.SEVERE, e.getMessage());
		}
	}

	public void editCandidatecomment(Candidatecomment candCom) throws Exception {
		try {
			candComEJB.mergeCandidatecomment(candCom);
			logger.info("inserted user:" + candCom.toString());
		} catch (Exception e) {
			logger.log(Level.SEVERE, e.getMessage());
		}
	}

	public void addCandidatecomment(Candidatecomment candCom) throws Exception {
		try {
			candComEJB.persistCandidatecomment(candCom);
			logger.info("inserted user:" + candCom.toString());
		} catch (Exception e) {
			logger.log(Level.SEVERE, e.getMessage());
		}
	}

	public List<Candidatecomment> getAllComments() {
		return candComEJB.getCandidatecommentFindAll();
	}

	public Vector<Candidatecomment> getAllCandidatecommentsByID(int id) {
		Vector<Candidatecomment> vector = new Vector<Candidatecomment>();
		for (Candidatecomment candidatecomment : candComEJB
				.getCandidatecommentFindAll()) {
			if (id == candidatecomment.getCandidate().getIdCandidate())
				vector.add(candidatecomment);
		}
		return vector;
	}

	public Candidate getCandidateById(int id) {
		return candEJB.getCandidateByID(id);
	}

	public List<Candidate> getCandidateFindAll() {
		return candEJB.getCandidateFindAll();
	}

	public List<Candidate> getCandidatesLimit(int startIndex, int endIndex) {
		return candEJB.getCandidateLimit(startIndex, endIndex);
	}

	public List<Candidate> getCandidates() {
		return candEJB.getCandidateFindAll();
	}

	public List<Candidate> filterByFirstName(String firstName) {
		return candEJB.filterByFirstName(firstName);
	}

	public List<Candidate> filterByLastName(String lastName) {
		return candEJB.filterByLastName(lastName);
	}

	public Vector<Candidate> getCandidatesForPosition(Position p) {
		Vector<Candidate> v = new Vector<Candidate>();
		List<Candidate> list = candEJB.getCandidateFindAll();
		for (Candidate c : list) {
			if (c.getPosition() != null)
				if (c.getPosition().getIdPosition() == p.getIdPosition()) {
					v.add(c);
				}
		}
		return v;
	}

	public int getTotal() {
		return candEJB.getTotal();
	}

	public Vector<Candidate> getCandidatesForUser(User u) {
		Vector<Candidate> v = new Vector<Candidate>();
		List<Candidate> listC = candEJB.getCandidateFindAll();
		for (Candidate c : listC) {
			if (c.getPosition().getIdUser() == u.getIdUser()) {
				v.add(c);
			}
		}
		return v;
	}
}
